<?php
class hrd_rapat_model extends MY_Model {
	var $nama_tabel = 'hrd_rapat';
	
	function GetJSON($idKaryawan, $idPeriode) {
		$ls_sql	= "	SELECT	a.id_rapat_pk, a.id_periode_fk, to_char(tgl_rapat, 'dd/mm/yyyy') tgl_rapat, a.ket_rapat, a.nilai
				FROM	".$this->nama_tabel." a
				WHERE 	a.id_karyawan_fk = '".$idKaryawan."'
				AND a.id_periode_fk = '".$idPeriode."'
				ORDER BY tgl_rapat";

		$list = $this->db->query($ls_sql)->result_array();

		// Ambil Jumlah
		$this->db->select_sum('nilai');
		$this->db->where('id_karyawan_fk', $idKaryawan);
		$this->db->where('id_periode_fk', $idPeriode);
		$sum = $this->db->get('hrd_rapat')->row_array();
		$footer[] = array('tgl_rapat' => '<b>Total</b>', 'nilai' => $sum['nilai']);
		$data = array('total' => count($list), 'rows' => $list, 'footer' => $footer);

		return json_encode( $data );
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_rapat_pk' => $arr_rule['id_rapat_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GenerateId() {
		$sql = "select * 
				from ( 
					select * 
					from hrd_rapat
					order by id_rapat_pk desc
					)
				where ROWNUM <= 1
				";
		$data = $this->db->query($sql)->row_array();

		if (count($data) == 0) {
			return 1;
		} else {
			return $data['id_rapat_pk']+1;
		}
	}

	function laporan() {
		$ls_sql = "select a.nama_karyawan, a.id_karyawan_pk,
					(
					    select count(*)
					    from hrd_rapat b
					    where B.ID_KARYAWAN_FK=a.id_karyawan_pk
					    AND b.tgl_rapat >= to_date('".post('tgl_awal')."', 'dd/mm/yyyy')
						AND b.tgl_rapat <= to_date('".post('tgl_akhir')."', 'dd/mm/yyyy')
					) as rapat,
					(
					    select sum(nilai)
					    from hrd_rapat c
					    where c.ID_KARYAWAN_FK=a.id_karyawan_pk
					    AND c.tgl_rapat >= to_date('".post('tgl_awal')."', 'dd/mm/yyyy')
						AND c.tgl_rapat <= to_date('".post('tgl_akhir')."', 'dd/mm/yyyy')
					) as nilai_rapat,
					(
					    select count(*)
					    from hrd_piket d
					    where d.ID_KARYAWAN_FK=a.id_karyawan_pk
					    AND d.tgl_piket >= to_date('".post('tgl_awal')."', 'dd/mm/yyyy')
						AND d.tgl_piket <= to_date('".post('tgl_akhir')."', 'dd/mm/yyyy')
					) as piket,
					(
					    select sum(nilai)
					    from hrd_piket e
					    where e.ID_KARYAWAN_FK=a.id_karyawan_pk
					    AND e.tgl_piket >= to_date('".post('tgl_awal')."', 'dd/mm/yyyy')
						AND e.tgl_piket <= to_date('".post('tgl_akhir')."', 'dd/mm/yyyy')
					) as nilai_piket
				from hrd_karyawan a
				WHERE a.id_karyawan_pk like '%".post('id_karyawan_pk')."%'
				ORDER BY ".(post('sort')=='' ? 'a.id_karyawan_pk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));

		return json_encode( $this->paging($ls_sql) );
		// $data = $this->db->query($ls_sql)->result_array();
		// echo json_encode($data);
				// print_r($ls_sql);
	}

}
?>